package com.example.annotationtest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.annotationtest.mapper.UserMapper;
import com.example.annotationtest.model.po.UserPO;
import com.example.annotationtest.model.vo.request.UserRequest;
import com.example.annotationtest.model.vo.request.UserUpdateRequest;
import com.example.annotationtest.model.vo.response.UserRespoonse;
import com.example.annotationtest.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/3/21 14:59
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {

    @Autowired
    private ThreadPoolTaskExecutor defaultThreadPool;

    @Override
    public Boolean saveUser(UserRequest request) {
        return this.save(convert(request));
    }

    @Override
    public UserRespoonse getUser(Long id) {
        return convertResponse(this.getById(id));
    }

    @Override
    @Transactional
    public Boolean updateAge(UserUpdateRequest userRequest) {
        UserPO user = this.getById(userRequest.getId());
        if (user != null) {
            user.setAge(user.getAge() + 1);
            this.updateById(user);
        }
        return true;
    }

    @Override
    public UserRespoonse getUserByName(String name) {
        defaultThreadPool.execute(this::doSth);
        defaultThreadPool.execute(this::doSth);
        defaultThreadPool.execute(this::doSth);
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getName, name);
        UserPO po = getOne(queryWrapper);
        if (Objects.isNull(po)) {
            return null;
        }
        return convertResponse(po);
    }

    private void doSth() {
        try {
            log.info("当前线程：" + Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private UserRespoonse convertResponse(UserPO userPO) {
        UserRespoonse userRespoonse = new UserRespoonse();
        userRespoonse.setId(userPO.getId());
        userRespoonse.setName(userPO.getName());
        userRespoonse.setAge(userPO.getAge());
        userRespoonse.setSalary(userPO.getSalary());
        userRespoonse.setAlias(userPO.getAlias());
        userRespoonse.setBirthday(userPO.getBirthday());
        userRespoonse.setStartworkdate(userPO.getStartworkdate());
        return userRespoonse;
    }

    private UserPO convert(UserRequest request) {
        UserPO userPO = new UserPO();
        userPO.setName(request.getName());
        userPO.setAge(request.getAge());
        userPO.setSalary(request.getSalary());
        userPO.setAlias(request.getAlias());
        userPO.setBirthday(request.getBirthday());
        userPO.setStartworkdate(request.getStartworkdate());
        return userPO;
    }
}
